#include "JPhysicsObject.h"
/*
JPhysicsObject::JPhysicsObject()
{
	m_mass            = 0.0f;
	m_theta           = 0.0f;
	m_objectType      = CTUNDEFINED;

	m_inertia[0]         = 0.0f;
	m_inertia[1]         = 0.0f;
	m_inertia[2]         = 0.0f;

	m_AABBaxes[0] = JPVector3(1.0f,0.0f,0.0f);
	m_AABBaxes[1] = JPVector3(0.0f,1.0f,0.0f);
	m_AABBaxes[2] = JPVector3(0.0f,0.0f,1.0f);
}

void JPhysicsObject::setCollisionType(CollisionType collisionType)
{
	m_objectType = collisionType;
}

void JPhysicsObject::update(float time)
{
	m_linearVelocity = m_linearVelocity + m_acceleration * time;
	m_position = m_position + m_linearVelocity * time;

	if(m_objectType == RADIAL)
	{
		// Update the angular distance for use in rotating sphere
		m_theta = m_theta + JPMaths::calcMagnitude(m_angularVelocity) * time;
		if(m_theta > 2.0f*PI)
			m_theta -= 2.0f*PI;

	}
	else if(m_objectType == OBB)
	{
		// Update the orientation matrix of the object according to its angular velocity
		float angle    = JPMaths::calcMagnitude(m_angularVelocity) * time;
		JPVector3 axis(0.0f,0.0f,0.0f);
		if(angle > 0.0f)
			axis = JPMaths::unitVector(m_angularVelocity);

		for(int i = 0; i < 3; i++)
		{
			m_axes[i] = JPMaths::quatRotation(m_axes[i], axis, angle);

			m_inertia[i] = (m_mass/3.0f) * (m_axes[i].x*m_axes[i].x*(m_e[1]*m_e[1] + m_e[2]*m_e[2])+ 
				m_axes[i].y*m_axes[i].y*(m_e[0]*m_e[0] + m_e[2]*m_e[2])+
				m_axes[i].z*m_axes[i].z*(m_e[0]*m_e[0] + m_e[1]*m_e[1]));

		}
		m_inertiaVec = JPVector3(m_inertia[0], m_inertia[1], m_inertia[2]);

	}
	else if(m_objectType == AABB)
	{
		computeAABBLengths(); // Compute the halflength of the AABB
		// Update the orientation matrix of the object according to its angular velocity
		float angle    = JPMaths::calcMagnitude(m_angularVelocity) * time;
		JPVector3 axis(0.0f,0.0f,0.0f);
		if(angle > 0.0f)
			axis = JPMaths::unitVector(m_angularVelocity);

		m_axes[0] = JPMaths::quatRotation(m_axes[0], axis, angle);
		m_axes[1] = JPMaths::quatRotation(m_axes[1], axis, angle);
		m_axes[2] = JPMaths::quatRotation(m_axes[2], axis, angle);
	}
}

void JPhysicsObject::setMass(float mass)
{
	m_mass = mass;
}

void JPhysicsObject::setRadius(float radius)
{
	m_radius = radius;
}

void JPhysicsObject::setPosition(JPVector3 *position)
{
	m_position = *position;
}

void JPhysicsObject::setLinearVelocity(JPVector3 *linearVelocity)
{
	m_linearVelocity = *linearVelocity;
}

void JPhysicsObject::setAcceleration(JPVector3 *acceleration)
{
	m_acceleration = *acceleration;
}

void JPhysicsObject::setAngularVelocity(JPVector3 *angularVel)
{
	m_angularVelocity = *angularVel;
}

void JPhysicsObject::setTheta(float theta)
{
	m_theta = theta;
}

void JPhysicsObject::setInertia(float* inertia)
{
	m_inertiaVec = JPVector3(inertia[0], inertia[1], inertia[2]);
}

void JPhysicsObject::setInertiaTensor(JPMatrix *inertiaTensor)
{
	m_inertiaTensor = *inertiaTensor;
}

void JPhysicsObject::translate(JPVector3 *translate)
{
	m_position = m_position + *translate;
}

void JPhysicsObject::setPlaneNormal(JPVector3 *normal)
{
	m_normal = *normal;
}

void JPhysicsObject::setOrientation(JPVector3 *orientation)
{
	for(int i = 0; i<3; i++)
		m_axes[i] = orientation[i];
}

void JPhysicsObject::setHalfLength(float *halfLength)
{
	for(int i = 0; i<3; i++)
		m_e[i] = halfLength[i];
}

void JPhysicsObject::computeAABBLengths()
{
	JPVector3 worldAxis[3];
	worldAxis[0] = JPVector3(1.0f,0.0f,0.0f);
	worldAxis[1] = JPVector3(0.0f,1.0f,0.0f);
	worldAxis[2] = JPVector3(0.0f,0.0f,1.0f);  

	for(int i = 0; i < 3; i++)
	{
		float s;
		JPVector3 point(0.0f, 0.0f, 0.0f);

		for(int j = 0; j < 3; j++)
		{
			s = JPMaths::dotProduct(m_axes[j] , worldAxis[i]);

			if(s < (-EPSILON))
				s = -1.0f;
			else if(s > (EPSILON))
				s = 1.0f;
			else
				s = 0.0f;

			point = point +	m_axes[j] * (s * m_e[j]);
		}
		m_AABBe[i] = JPMaths::dotProduct(point, worldAxis[i]);
	}
}
*/